﻿exports.Commons = {
    finishDBAndSetResult: function (db, dbType, res, isSuccess,isOnlyCommit) {
        var result = {};
        var RESULT_SUCCESS = 0;
        var RESULT_ERROR = -1;
        var RESULT_SUCCESS_WITH_CLEAR = 1;
        
        if (dbType == "mongo") {
            DBUtils = require("../common/MongoDBUtils").DBUtils;
        } else if (dbType == "mysql") {
            DBUtils = require("../common/MysqlDBUtils").DBUtils;
        }
        if (!isOnlyCommit) {
            if (isSuccess) {
                result = { code: RESULT_SUCCESS_WITH_CLEAR, interval: 60000, message: null };
            } else {
                result = { code: RESULT_ERROR, interval: 10000, message: "save data error." };
            }
            res.send(JSON.stringify(result));
        }
        db.commit(function (err) {
            if (err) {
                db.rollback(function () {
                    console.log(err);
                    DBUtils.close(db);
                });
            } else {
                DBUtils.close(db);
            }
        });
    }
};